﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSOL.FastSPWeb.ValueObjects;
using System.Threading;
using Microsoft.SharePoint;

namespace TSOL.FastSPWeb.Core
{
    internal partial class FastSPWebThreadWorkerQueue 
    {
        internal void QueueOpenWeb(Guid inSiteID, Guid inWebID, Action<SPSite, SPWeb, string> outProc)
        {
            if (SPContext.Current != null && SiteDictionary.Count < 1)
            {
                SiteDictionaryHelper.Build(SPContext.Current.Site);
            }

            lock (locker)
            {
                taskQ.Enqueue(new FastSPWebQueueClass() { UserUniqueID = null, UserToken = null, SiteGuid = inSiteID, WebGuid = inWebID, Proc = outProc });
                Monitor.Pulse(locker);
            }
        }

        internal void QueueOpenWeb(SPUser user, Guid inSiteID, Guid inWebID, Action<SPSite, SPWeb, string> outProc)
        {
            if (SPContext.Current != null && SiteDictionary.Count < 1)
            {
                SiteDictionaryHelper.Build(SPContext.Current.Site);
            }

            lock (locker)
            {
                taskQ.Enqueue(new FastSPWebQueueClass() { UserUniqueID = user.LoginName.GetHashCode().ToString(), UserToken = user.UserToken, SiteGuid = inSiteID, WebGuid = inWebID, Proc = outProc });
                Monitor.Pulse(locker);
            }
        }

        internal void QueueOpenWeb(string inSiteUrl, string inWebUrl, Action<SPSite, SPWeb, string> outProc)
        {
            if (inSiteUrl == null && inWebUrl == null && outProc == null)
            {
                lock (locker)
                {
                    taskQ.Enqueue(new FastSPWebQueueClass() { UserUniqueID = null, UserToken = null, Proc = null });
                    Monitor.Pulse(locker);
                }
                return;
            }

            
            if (SPContext.Current != null && SiteDictionary.Count < 1)
            {
                SiteDictionaryHelper.Build(SPContext.Current.Site);
            }

            lock (locker)
            {
                Guid WebGuid = Guid.Empty;
                SPSiteEntryClass siteEntry = null;
                Guid SiteGuid = Guid.Empty;
                if (inSiteUrl.EndsWith("/")) inSiteUrl = inSiteUrl.Remove(inSiteUrl.Length - 1);
                int _SiteUrlHashCode = inSiteUrl.GetHashCode();
                if (!SiteDictionary.TryGetValue(_SiteUrlHashCode, out siteEntry))
                {
                    throw new SPException("Site(URL) does not exist (not found in dictionary) : " + inSiteUrl);
                }


                if (inWebUrl.EndsWith("/")) inWebUrl = inWebUrl.Remove(inWebUrl.Length - 1);
                if (!inWebUrl.StartsWith("/")) inWebUrl = "/" + inWebUrl;

                if (!siteEntry.WebDictionary.TryGetValue(inWebUrl, out WebGuid))
                {
                    throw new SPException("Web(URL) does not exist (not found in dictionary) : " + inWebUrl);
                }
                taskQ.Enqueue(new FastSPWebQueueClass() { UserUniqueID = null, UserToken = null, SiteGuid = siteEntry.SiteID, WebGuid = WebGuid, Proc = outProc });
                Monitor.Pulse(locker);
            }
        }
        internal void QueueOpenWeb(SPUser user, string inSiteUrl, string inWebUrl, Action<SPSite, SPWeb, string> outProc)
        {
            if (SPContext.Current != null && SiteDictionary.Count < 1)
            {
                SiteDictionaryHelper.Build(SPContext.Current.Site);
            }

            lock (locker)
            {
                Guid WebGuid = Guid.Empty;
                SPSiteEntryClass siteEntry = null;
                Guid SiteGuid = Guid.Empty;
                int _SiteUrlHashCode = inSiteUrl.GetHashCode();
                if (!SiteDictionary.TryGetValue(_SiteUrlHashCode, out siteEntry))
                {
                    throw new SPException("Site(URL) does not exist (not found in dictionary) : " + inSiteUrl);
                }

                if (inWebUrl.EndsWith("/")) inWebUrl = inWebUrl.Remove(inWebUrl.Length - 1);
                if (!inWebUrl.StartsWith("/")) inWebUrl = "/" + inWebUrl;
                if (!siteEntry.WebDictionary.TryGetValue(inWebUrl, out WebGuid))
                {
                    throw new SPException("Web(URL) does not exist (not found in dictionary) : " + inWebUrl);
                }
                taskQ.Enqueue(new FastSPWebQueueClass() { UserUniqueID = user.LoginName.GetHashCode().ToString(), UserToken = user.UserToken, SiteGuid = siteEntry.SiteID, WebGuid = WebGuid, Proc = outProc });
                Monitor.Pulse(locker);
            }
        }


    }
}
